package util.android.widgets;

import util.form.respuesta.Input;
import util.form.respuesta.View0;

import com.androidutil.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class AbstractView<I extends Input, V> 
extends LinearLayout
implements View0
{
	
	private final LayoutPregunta contenedor;
	
	private final CheckBox cbHabilitable;
	
	private final LinearLayout lyPregunta;
	private final LinearLayout lyEtiqueta;
	
	private final LinearLayout lyRespuesta;
	
	private final TextView tvEtiqueta;
	private final TextView tvIndicacion;
	private final I input;
	
	public AbstractView(Context context, AttributeSet attrs) {
		super(context,attrs);
		
		
		contenedor = (LayoutPregunta) View.inflate(getContext(), R.layout.layout_pregunta_respuesta1, null);
		lyPregunta = (LinearLayout) contenedor.findViewWithTag("lyPregunta");
		lyEtiqueta = (LinearLayout) contenedor.findViewWithTag("lyEtiqueta");
		lyRespuesta = (LinearLayout) contenedor.findViewWithTag("lyRespuesta");
		//lyView = (LinearLayout) contenedor.findViewWithTag("lyView");
		
		tvEtiqueta = (TextView) contenedor.findViewWithTag("etiqueta");
		tvIndicacion = (TextView) contenedor.findViewWithTag("indicacion");
		
		cbHabilitable = new CheckBox(context);
		{
			//cbHabilitable.setGravity(Gravity.CENTER_HORIZONTAL);
			LinearLayout.LayoutParams params = 
					new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			//params.weight = 1.0f;
			params.gravity = Gravity.CENTER_HORIZONTAL;		
			lyPregunta.addView(cbHabilitable,params);
			setHabilitable(false);
			
			cbHabilitable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					onHabilitableChanged(isChecked);
				}				
			});
		}
		
		input = crearInput_agregarVistas();
		input.setContenedor(contenedor);
		
		//---------------------------
		//LAYOUT PARAMS
		//---------------------------
		
		LinearLayout.LayoutParams params = new LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, 
				LinearLayout.LayoutParams.MATCH_PARENT
		);		
		
		/*
		Resources r = getResources();		
		this.setBackgroundColor(r.getColor(R.color.azul_oscuro));
		int m = (int) r.getDimension(R.dimen.pregunta_border);
		params.setMargins(m, m, m, 0);
		*/
        addView(contenedor, params);
        
        procesarAtributosPersonalizados(context,attrs);
    }
	
	protected void procesarAtributosPersonalizados(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AbstractView, 0, 0);
	    try {
	        String etiqueta = ta.getString(R.styleable.AbstractView_etiqueta);
	        //tvEtiqueta.setText(etiqueta);
	        setEtiqueta(etiqueta);
	        
	        String indicacion = ta.getString(R.styleable.AbstractView_indicacion);
	        tvIndicacion.setText(indicacion);
	    } finally {
	        ta.recycle();
	    }
	}
		


	
	/*
	public final AbstractView<I> removerView() {
		View view = viewGroup.findViewWithTag("view");
		viewGroup.removeView(view);
		return this;
	}*/
	
	protected abstract I crearInput_agregarVistas();
			
	public final I getInput() {
		return input;
	}
	
	@SuppressWarnings("unchecked")
	public final V setEtiqueta(String etiqueta) {
		tvEtiqueta.setText(etiqueta);
		input.setEtiquetaCorta(etiqueta);
		return (V) this;
	}
	
	public final V setEtiquetaByStringId(int etiquetaID) {
		return setEtiqueta(getResources().getString(etiquetaID));
	}

	public LinearLayout getLayoutPregunta() {
		return lyPregunta;
	}
	
	public LinearLayout getLayoutEtiqueta() {
		return lyEtiqueta;
	}

	public LinearLayout getLayoutRespuesta() {
		return lyRespuesta;
	}
	
	
	@SuppressWarnings("unchecked")
	public final V setHabilitable(boolean habilitable) {
		if(habilitable) {
			cbHabilitable.setVisibility(View.VISIBLE);
			onHabilitableChanged(cbHabilitable.isChecked());			
		}
		else{
			cbHabilitable.setVisibility(View.GONE);
		}		
		return (V)this;
	}
	
	protected void onHabilitableChanged(boolean isChecked) {
		this.getInput().setRelevancia(isChecked);
	}


	public ViewGroup getContenedor() {
		return contenedor;
	}
	
	public String getEtiqueta() {
		return tvEtiqueta.getText().toString();
	}
}
